﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 学生成绩管理系统
{
    public partial class Frmaddcj : Form
    {
        public Frmaddcj()
        {
             InitializeComponent();
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
           
            string sno = this.TxtStuid.Text;
            string cno = this.TxtKcid.Text;
            string gradepeacetime = this.txtGradepeacetime.Text;
            string gradeexpriment = this.txtGradepeacetime.Text;
            string gradelast = this.txtGradeexpriment.Text;
            string grade = this.txtGrade.Text;
            if (sno == null || sno.Trim().Equals(""))
            {//学号不允许输入为空
                MessageBox.Show("请输入学号！", "提示");
                this.TxtStuid.Focus();
                return;
            }
            if (cno == null || cno.Trim().Equals(""))
            {//课程号不允许输入为空
                MessageBox.Show("请输入课程编号！", "提示");
                this.TxtKcid.Focus();
                return;
            }
            if (gradepeacetime != null && !gradepeacetime.Trim().Equals(""))
            {//平时不允许输入为空，并且必须为数字
                try
                {
                    decimal.Parse(gradepeacetime);
                }
                catch (Exception ex)
                {
                    ex.ToString();
                    MessageBox.Show("平时成绩请输入数字！", "提示");
                    this.txtGradepeacetime.Focus();
                    return;
                }
            }
            if (gradeexpriment != null && !gradeexpriment.Trim().Equals(""))
            {//实验成绩不允许输入为空，并且必须位数字
                try
                {
                    decimal.Parse(gradeexpriment);
                }
                catch (Exception ex)
                {
                    ex.ToString();
                    MessageBox.Show("实验成绩请输入数字！", "提示");
                    this.txtGradeexpriment.Focus();
                    return;
                }
            }
            if (gradelast != null && !gradelast.Trim().Equals(""))
            {//期末成绩不允许输入为空，并且必须为数字
                try
                {
                    decimal.Parse(gradelast);
                }
                catch (Exception ex)
                {
                    ex.ToString();
                    MessageBox.Show("期末成绩请输入数字！", "提示");
                    this.TxtGradelast.Focus();
                    return;
                }
            }
            StuGradeData data1 = new StuGradeData();
            data1.Sno = sno;
            data1.Cno = cno;
            data1.Gradepeacetime = gradepeacetime;
            data1.Gradeexpriment = gradeexpriment;
            data1.Gradelast = gradelast;
            data1.Grade = grade;
            try
            {
                if (TxtStuid.Text == "" || TxtKcid.Text == "")
                {
                    if (StuGradeOperation.inserStuGrade(data1))
                    {//调用添加成绩信息函数
                        MessageBox.Show("修改成功！", "提示");
                        this.TxtStuid.Text = "";
                        this.txtGradepeacetime.Text = "";
                        this.TxtGradelast.Text = "";
                        this.txtGrade.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("修改失败！", "错误");
                    }
                }
                else
                {
                    if (StuGradeOperation.updateStuGrade(data1))
                    {//调用修改成绩函数
                        MessageBox.Show("修改成功！", "提示");
                        this.TxtKcid.Text = "";
                        this.TxtKcname.Text = "";
                        this.TxtStuid.Text = "";
                        this.TxtStuname.Text = "";
                        this.txtGrade.Text = "";
                        this.txtGradeexpriment.Text = "";
                        this.TxtGradelast.Text = "";
                        this.txtGradepeacetime.Text = "";
                        this.TxtStuid.Focus();
                    }
                    else
                    {
                        MessageBox.Show("修改失败！", "错误");
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
                MessageBox.Show("保存失败!", "错误");
            }

        }

        private void TxtStuid_TextChanged(object sender, EventArgs e)
        {
            //DataSet dsStu = null;
            if (this.TxtStuid.Text.Trim() != "")
            {
                StuentInfoData data = new StuentInfoData();
                data.Sno = this.TxtStuid.Text;
                DataSet deStu = StuentInfoOperation.getStuentInfo(data);
                if (deStu.Tables[0].Rows.Count > 0)
                {
                    this.TxtStuname.Text = deStu.Tables[0].Rows[0]["姓名"].ToString();
                }
            }
        }

        private void TxtKcid_TextChanged(object sender, EventArgs e)
        {//获取对应课程号的课程名称
            if (this.TxtKcid.Text.Trim() != "")
            {
                CourseInfoData data = new CourseInfoData();
                data.Kcid = this.TxtKcid.Text;
                DataSet dsKc = CourseInfoOperation.getCourseInfo(data);
                if (dsKc.Tables[0].Rows.Count > 0)
                {
                    this.TxtKcname.Text = dsKc.Tables[0].Rows[0]["课程名称"].ToString();
                }
            }

        }

        private void Frmaddcj_Load(object sender, EventArgs e)
        {
            this.TxtStuid.Text = Frmcjcx.s;
            this.TxtKcid.Text = Frmcjcx.c;
            if (this.TxtStuid.Text.Trim() != "" && this.TxtKcid.Text.Trim() != "")
            {
                StuGradeData data = new StuGradeData();
                data.Sno = this.TxtStuid.Text;
                data.Cno = this.TxtKcid.Text;
                DataSet deStu = StuGradeOperation.getStuGrade(data);
                if (deStu.Tables[0].Rows.Count > 0)
                {
                    this.txtGradeexpriment.Text = deStu.Tables[0].Rows[0]["实验成绩"].ToString();
                    this.txtGradepeacetime.Text = deStu.Tables[0].Rows[0]["平时成绩"].ToString();
                    this.TxtGradelast.Text = deStu.Tables[0].Rows[0]["期末成绩"].ToString();
                    this.txtGrade.Text = deStu.Tables[0].Rows[0]["总成绩"].ToString();

                }
            }

        }
    }
    //CourseInfoOperation
    public class CourseInfoOperation
    {
        private static DataAccess dataAccess1 = new DataAccess();
        public static bool insertCourseInfo(CourseInfoData courseInfoData)
        {//向数据库中CourseInfo表插入课程信息记录
            string sql = "insert into 课程信息表(课程编号,课程名称,实验学时,讲课学时,总学分,课程类型)values(" + 
                "'" +courseInfoData.Kcid + "'," +
                "'" + courseInfoData.Kcname + "'," +
                 "'" + courseInfoData.Periodexpriment + "'," +
                  "'" + courseInfoData.Periodteaching + "'," +
                   "'" + courseInfoData.Credit + "'," +
                    "'" + courseInfoData.Coursetype + "')";
            return dataAccess1.ExecuteSQL(sql);//执行sql语句
        }
        public static bool updateCourseInfo(CourseInfoData courseInfoData)
        {
            //修改课程信息记录
            string sql = "update 课程信息表 set 课程名称='" + courseInfoData.Kcname + "'," +
                "实验学时='" + courseInfoData.Periodexpriment + "'," +
                "讲课学时='" + courseInfoData.Periodteaching + "'," +
                "总学分='" + courseInfoData.Credit + "'," +
                "课程类型='" + courseInfoData.Coursetype + "'" +
                "where 课程编号='" + courseInfoData.Kcid + "'";
            return dataAccess1.ExecuteSQL(sql);//执行sql语句
        }
        public static bool deleteCourseInfo(string kcid)
        {//删除课程信息记录
            string sql = String.Format("delete 课程信息表 where 课程编号='{0}'", kcid);
            return dataAccess1.ExecuteSQL(sql);//执行sql语句
        }
        public static DataSet getCourseInfo(CourseInfoData courseInfoData)
        {//获取课程信息
            string condition = "";//查询课程信息的条件
            if (courseInfoData.Kcid != null && courseInfoData.Kcid != "")
            {
                condition += "and 课程编号='" + courseInfoData.Kcid + "'";

            }
            if (courseInfoData.Kcname != null && courseInfoData.Kcname != "")
            {
                condition += "and 课程名称='" + courseInfoData.Kcname + "'";
            }
            if (courseInfoData.Coursetype != null && courseInfoData.Coursetype != "")
            {
                condition += " and 课程类型='" + courseInfoData.Coursetype+"'";
            }
            if (courseInfoData.Credit != null && courseInfoData.Credit != "")
            {
                condition += " and 总学分='" + courseInfoData.Credit+"'";
            }
            string sql = "select 课程编号,课程名称,实验学时,讲课学时,总学分,课程类型 from 课程信息表 where 1=1" + condition;
            return dataAccess1.GetDataSet(sql, "课程信息表");//执行sql语句
        }

    }

    ////StuentInfoData
    //public class StuentInfoData
    //{
    //    private string sno = "";
    //    private string sname = "";
    //    private string sex = "";
    //    private string birthday = "";
    //    private string address = "";
    //    private string tel = "";
    //    private string classid = "";
    //    public string Sno//学号
    //    {
    //        get { return sno; }
    //        set { sno = value; }
    //    }
    //    public string Sname//学生姓名
    //    {
    //        get { return sname; }
    //        set { sname = value; }
    //    }
    //    public string Sex//学生性别
    //    {
    //        get { return sex; }
    //        set { sex = value; }
    //    }
    //    public string Birthday//出生日期
    //    {
    //        get { return birthday; }
    //        set { birthday = value; }
    //    }
    //    public string Address//地址
    //    {
    //        get { return address; }
    //        set { address = value; }
    //    }
    //    public string Tel//电话号码
    //    {
    //        get { return tel; }
    //        set { tel = value; }
    //    }
    //    public string Classid//班级名称
    //    {
    //        get { return classid; }
    //        set { Classid = value; }
    //    }
    //}
    ////StuentInfoOperation
    //public class StuentInfoOperation
    //{
    //    private static DataAccess dataAccess = new DataAccess();
    //    public static bool insertStuentInfo(StuentInfoData stuentInfoData)
    //    {//数据插入
    //        string sql = "insert into 学生信息表(学号,姓名,性别,出生日期,地址,电话,所在班级)values(" +
    //            "'" + stuentInfoData.Sno + "'," +
    //            "'" + stuentInfoData.Sname + "'," +
    //            "'" + stuentInfoData.Sex + "'," +
    //            "'" + stuentInfoData.Birthday + "'," +
    //            "'" + stuentInfoData.Address + "'," +
    //            "'" + stuentInfoData.Tel + "'," +
    //            "'" + stuentInfoData.Classid + "')";
    //        return dataAccess.ExecuteSQL(sql);//执行sql语句
    //    }
    //    public static bool updateStuentInfo(StuentInfoData stuentInfoData)
    //    {//修改数据
    //        string sql = "update 学生信息表 set" +
    //            "姓名='" + stuentInfoData.Sname + "'," +
    //            "性别='" + stuentInfoData.Sex + "'," +
    //            "出生日期='" + stuentInfoData.Birthday + "'," +
    //            "地址='" + stuentInfoData.Address + "'," +
    //            "电话='" + stuentInfoData.Tel + "'," +
    //            "所在班级='" + stuentInfoData.Classid + "' where 学号='" +
    //            stuentInfoData.Sno + "'";
    //        return dataAccess.ExecuteSQL(sql);//执行sql语句
    //    }
    //    public static bool deleteStuentInfo(string sno)
    //    {//删除数据
    //        string sql = String.Format("delete 学生信息表 where 学号='{0}'", sno);
    //        return dataAccess.ExecuteSQL(sql);//执行sql语句
    //    }
    //    public static DataSet getStuentInfo(StuentInfoData stuentInfoData)
    //    {//数据查询
    //        string condition = "";//查询的条件
    //        if (stuentInfoData.Sno != null && stuentInfoData.Sno != "")
    //        {
    //            condition += "and 学号='" + stuentInfoData.Sno + "'";
    //        }
    //        if (stuentInfoData.Sname != null && stuentInfoData.Sname != "")
    //        {
    //            condition += "and 姓名 like '%" + stuentInfoData.Sname + "%'";
    //        }
    //        if (stuentInfoData.Sex != null && stuentInfoData.Sex != "")
    //        {
    //            condition += "and 性别='" + stuentInfoData.Sex + "'";
    //        }
    //        if (stuentInfoData.Classid != null && stuentInfoData.Classid != "")
    //        {
    //            condition += " and 所在班级='" + stuentInfoData.Classid + "'";
    //        }
    //        string sql = "select 学号,姓名,性别,出生日期,地址,电话,所在班级 from 学生信息表 where 1=1" + condition;
    //        return dataAccess.GetDataSet(sql, "学生信息表");//执行sql语句

    //    }
    //}
}